package org.apache.zookeeper.server.paxos2;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.Serializable;

/**
 * Created by IntelliJ IDEA.
 * User: wangw
 * Date: 2011-6-12
 * Time: 21:43:08
 * To change this template use File | Settings | File Templates.
 */
public class FetchBlocks extends Message {
	public int sid;
    public int targetSid;
    public long zxid;
	public long startBlock=-1;
	public long endBlock=-1;
	public long interval=1;
	
	public long[] blockIds;
	
	@Override
	public void deserialize(DataInputStream in) throws IOException {
		sid = in.readInt();
        targetSid = in.readInt();
        zxid = in.readLong();
		startBlock = in.readLong();
		endBlock = in.readLong();
		interval = in.readLong();
		
		int size = in.readInt();
		blockIds = new long[size];
		for(int i=0; i<size; i++)
			blockIds[i] = in.readLong();
	}
	@Override
	public void serialize(DataOutputStream out) throws IOException {
		out.writeInt(sid);
        out.writeInt(targetSid);
        out.writeLong(zxid);
		out.writeLong(startBlock);
		out.writeLong(endBlock);
		out.writeLong(interval);
		if(blockIds==null){
			out.writeInt(0);
			return;
		}
		out.writeInt(blockIds.length);
		for(int i=0; i<blockIds.length; i++)
			out.writeLong(blockIds[i]);
		
	}
	
	public String toString(){
        if(startBlock!=-1)
            return "FetchBlocks sid="+sid+" target="+targetSid+" zxid="+zxid+" start="+startBlock+" end="+endBlock+" interval="+interval;
        else
            return "FetchBlocks sid="+sid+" target="+targetSid+" zxid="+zxid+" start="+blockIds[0]+" end="+blockIds[blockIds.length-1]+" length="+blockIds.length;
    }

}
